package com.gzwanchuang.xhgj.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gzwanchuang.xhgj.R;
import com.huawei.hms.hmsscankit.OnResultCallback;
import com.huawei.hms.hmsscankit.RemoteView;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;

public class ScanActivity extends AppCompatActivity {

    FrameLayout frameLayout;
    int mScreenWidth;
    int mScreenHeight;
    RemoteView remoteView;
    ImageView iv_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        // 绑定相机预览布局。
        frameLayout = findViewById(R.id.rim);
        iv_back = findViewById(R.id.iv_back);

        // 设置扫码识别区域，您可以按照需求调整参数。
        DisplayMetrics dm = getResources().getDisplayMetrics();
        float density = dm.density;
        mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        mScreenHeight = getResources().getDisplayMetrics().heightPixels;


        // 当前Demo扫码框的宽高是300dp。
        final int SCAN_FRAME_SIZE = 300;
        final int SCAN_FRAME_HEIGHT = 50;
        int scanFrameSize = (int) (SCAN_FRAME_SIZE * density);
        int scanFrameSizeHeight = (int) (SCAN_FRAME_HEIGHT * density);
        Rect rect = new Rect();
        rect.left = mScreenWidth / 2 - scanFrameSize / 2;
        rect.right = mScreenWidth / 2 + scanFrameSize / 2;
        rect.top = mScreenHeight / 2 - scanFrameSizeHeight / 2;
        rect.bottom = mScreenHeight / 2 + scanFrameSizeHeight / 2;

        // 初始化RemoteView，并通过如下方法设置参数:setContext()（必选）传入context、setBoundingBox()设置扫描区域、setFormat()设置识别码制式，设置完毕调用build()方法完成创建。通过setContinuouslyScan（可选）方法设置非连续扫码模式。
         remoteView = new RemoteView.Builder().setContext(this).setBoundingBox(rect).setFormat(HmsScan.CODE128_SCAN_TYPE).build();
        // 将自定义view加载到activity的frameLayout中。
        remoteView.onCreate(savedInstanceState);
        remoteView.setBackgroundColor(Color.parseColor("#000000"));
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        frameLayout.addView(remoteView, params);

        // 识别结果回调事件订阅
        remoteView.setOnResultCallback(new OnResultCallback() {
            @Override
            public void onResult(HmsScan[] result) {
                // 获取到扫码结果HmsScan
                Intent intent = new Intent();
                intent.putExtra(ScanUtil.RESULT,result[0].getOriginalValue()+"");
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    protected void onStart() {
        super.onStart();
        // 侦听activity的onStart
        remoteView.onStart();
    }
    @Override
    protected void onResume() {
        super.onResume();
        // 侦听activity的onResume
        remoteView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        // 侦听activity的onPause
        remoteView.onPause();
    }
    @Override
    protected void onStop() {
        super.onStop();
        // 侦听activity的onStop
        remoteView.onStop();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 侦听activity的onDestroy
        remoteView.onDestroy();
    }
}